import java.io.File;
import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要搜索的目录");
        String dir = scanner.next();
        File dirFile = new File(dir);
        if(!dirFile.isDirectory()){
            System.out.println("您输入的不是目录");
            return;
        }
        System.out.println("请输入您要搜索的字符");
        String keyWord = scanner.next();

        scanDir(dirFile,keyWord);
    }

    private static void scanDir(File dirFile, String keyWord) {
        File[] files = dirFile.listFiles();
        if(files == null){
            return;
        }
        for (File file:files) {
            System.out.println("遍历目录&文件"+file.getAbsolutePath());
            if(file.isFile()){
                dealFile(file,keyWord);
            }else {
                scanDir(file,keyWord);
            }
        }
    }

    private static void dealFile(File file, String keyWord) {
        if(file.getName().contains(keyWord)){
            System.out.println("发现文件"+file.getAbsolutePath()+"是否删除Y/N");
            Scanner scanner = new Scanner(System.in);
            String string = scanner.next();
            if(string.equalsIgnoreCase("y")){
                file.delete();
                System.out.println("已删除");
            }
        }
    }
}
